package com.qyhl.module_home.home.search;


import android.util.Log;

import com.qyhl.module_home.common.HomeUrl;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.home.NewsBean;

import java.util.List;

/**
 * Created by qyhl on 2017/11/17.
 */

public class SearchModel implements SearchContract.SearchModel {
    private SearchPresenter mPresenter;

    public SearchModel(SearchPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    /**
     * 获取搜索结果
     *
     * @param keyword 关键字
     * @param newsId  分页新闻id
     */
    @Override
    public void getData(String keyword, final String newsId) {
        EasyHttp.get(HomeUrl.GET_SEARCH_NEWS)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("title", keyword)
                .params("ID", newsId)
                .execute(new SimpleCallBack<List<NewsBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 216) {
                            if ("0".equals(newsId)) {
                                mPresenter.setError(2, "搜索包含敏感字！");
                            } else {
                                mPresenter.setError(4, "搜索包含敏感字！");
                            }
                        } else {
                            if ("0".equals(newsId)) {//刷新
                                mPresenter.setError(1, "网络异常，搜索内容失败！");
                            } else {
                                mPresenter.setError(2, "网络异常，搜索内容失败！");
                            }
                        }

                    }

                    @Override
                    public void onSuccess(List<NewsBean> newsBeans) {
                        if (newsBeans.size() > 0) {
                            if ("0".equals(newsId)) {
                                mPresenter.setData(newsBeans, false);
                            } else {
                                mPresenter.setData(newsBeans, true);
                            }
                        } else {
                            if ("0".equals(newsId)) {
                                mPresenter.setError(3, "搜索不到相应的信息！");
                            } else {
                                mPresenter.setError(4, "暂无更多内容！");
                            }
                        }
                    }
                });
    }

    @Override
    public void getList(String keyword, String newsId, String catalogId, int subCatalog) {
        EasyHttp.get(HomeUrl.GET_SEARCH_LIST)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("title", keyword)
                .params("catalogId", catalogId)
                .params("subCatalog", subCatalog + "")
                .params("ID", newsId)
                .execute(new SimpleCallBack<List<NewsBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 216) {
                            if ("0".equals(newsId)) {
                                mPresenter.setError(2, "搜索包含敏感字！");
                            } else {
                                mPresenter.setError(4, "搜索包含敏感字！");
                            }
                        } else {
                            if ("0".equals(newsId)) {//刷新
                                mPresenter.setError(1, "搜索内容失败！");
                            } else {
                                mPresenter.setError(2, "搜索内容失败！");
                            }
                        }

                    }

                    @Override
                    public void onSuccess(List<NewsBean> newsBeans) {
                        if (newsBeans.size() > 0) {
                            if ("0".equals(newsId)) {
                                mPresenter.setData(newsBeans, false);
                            } else {
                                mPresenter.setData(newsBeans, true);
                            }
                        } else {
                            if ("0".equals(newsId)) {
                                mPresenter.setError(3, "搜索不到相应的信息！");
                            } else {
                                mPresenter.setError(4, "暂无更多内容！");
                            }
                        }
                    }
                });
    }
}
